import com.adobe.cairngorm.control.CairngormEventDispatcher;

import mx.events.CloseEvent;
import mx.events.ValidationResultEvent;
import mx.managers.PopUpManager;

import net.optix.ip.events.ClientRegisterEvent;
import net.optix.ip.events.PractitionerRegisterEvent;
import net.optix.ip.model.ModelLocator;
import net.optix.ip.vo.PractitionerRegisterVO;
import net.optix.ip.vo.RegisterVO;
            
[Bindable]
public var model:ModelLocator	=	ModelLocator.getInstance();
  
private function titleWindow_close(evt:CloseEvent):void 
{ 
	model.result	=	"";
	model.bSubmit	=	true;
	try
	{
		this.parent.removeChild(this);
	}
	catch(error:Error){}
	
} 
            
public function init():void
{
	model.bSubmit	=	true;
  	model.result	=	"";
  	model.close		=	"no";  			
}
  			
            private function showClientRegister():void
            {
            	registerClientPractitioner.selectedIndex	=	0;
            }
            
            private function showPractitionerRegister():void
            {
            	registerClientPractitioner.selectedIndex	=	1;
            }
            
            private function clientRegister():void
            {
            	var result:Boolean	=	true;

				var resultValidate:ValidationResultEvent;
				
				resultBusinessClient.text	=	"";
				resultEmailClient.text		=	"";
				resultPasswordClient.text	=	"";
				resultRetryPasswordClient.text	=	"";
				
				resultValidate	=	stringValidator.validate(businessClient.text);
            	if(resultValidate.type == ValidationResultEvent.INVALID)
            	{
            		result	=	false;
            		resultBusinessClient.text	=	
            			resultValidate.message;
            	}
            	
            	resultValidate	=	emailValidator.validate(emailClient.text);
            	if(resultValidate.type == ValidationResultEvent.INVALID)
            	{
            		result	=	false;
            		resultEmailClient.text	=	
            			resultValidate.message;
            	}
            	
            	stringValidator.minLength	=	6;
            	resultValidate	=	stringValidator.validate(passwordClient.text);
            	if(resultValidate.type == ValidationResultEvent.INVALID)
            	{
            		result	=	false;
            		resultPasswordClient.text	=	
            			resultValidate.message;
            	}
            	
            	if(retryPasswordClient.text != passwordClient.text)
            	{
            		result	=	false;
            		resultRetryPasswordClient.text	=	"It is not same as the password";
            	}
            	
            	stringValidator.minLength	=	0;
            	
            	model.bSubmit	=	false;
            	model.result	=	"";
            	
            	
            	
            	if(result == true)
            	{
            		var vo:RegisterVO	=	new RegisterVO();
            		vo.name		=	businessClient.text;
            		vo.email	=	emailClient.text;
            		vo.password	=	passwordClient.text;
            		var event:ClientRegisterEvent	=	new ClientRegisterEvent(vo);
            		CairngormEventDispatcher.getInstance().dispatchEvent( event );
            	}
            	else
            	{
            		model.bSubmit	=	true;
            	}
            	
            }
            
            private function clientClear():void
            {
            	businessClient.text	=	"";
            	emailClient.text	=	"";
            	passwordClient.text	=	"";
            	retryPasswordClient.text	=	"";
            	
            	resultBusinessClient.text	=	"";
				resultEmailClient.text		=	"";
				resultPasswordClient.text	=	"";
				resultRetryPasswordClient.text	=	"";
            	
            	model.result	=	"";
            }
            
            private function practitionerRegister():void
            {
            	var result:Boolean	=	true;

				var resultValidate:ValidationResultEvent;
				
				resultNamePractitioner.text	=	"";
				resultEmailPractitioner.text		=	"";
				resultPasswordPractitioner.text	=	"";
				resultRetryPasswordPractitioner.text	=	"";
				
				resultValidate	=	stringValidator.validate(namePractitioner.text);
            	if(resultValidate.type == ValidationResultEvent.INVALID)
            	{
            		result	=	false;
            		resultNamePractitioner.text	=	
            			resultValidate.message;
            	}
            	
            	resultValidate	=	emailValidator.validate(emailPractitioner.text);
            	if(resultValidate.type == ValidationResultEvent.INVALID)
            	{
            		result	=	false;
            		resultEmailPractitioner.text	=	
            			resultValidate.message;
            	}
            	
            	stringValidator.minLength	=	6;
            	resultValidate	=	stringValidator.validate(passwordPractitioner.text);
            	if(resultValidate.type == ValidationResultEvent.INVALID)
            	{
            		result	=	false;
            		resultPasswordPractitioner.text	=	
            			resultValidate.message;
            	}
            	
            	if(retryPasswordPractitioner.text != passwordPractitioner.text)
            	{
            		result	=	false;
            		resultRetryPasswordPractitioner.text	=	"It is not same as the password";
            	}
            	
            	stringValidator.minLength	=	0;
            	
            	model.bSubmit	=	false;
            	model.result	=	"";
            	
            	
            	
            	if(result == true)
            	{
            		model.bSubmit	=	false;
            		var vo:RegisterVO	=	new RegisterVO();
            		vo.name		=	namePractitioner.text;
            		vo.email	=	emailPractitioner.text;
            		vo.password	=	passwordPractitioner.text;
            		var event:PractitionerRegisterEvent	=	new PractitionerRegisterEvent(vo);
            		CairngormEventDispatcher.getInstance().dispatchEvent( event );
            	}
            	else
            	{
            		model.bSubmit	=	true;
            	}
            }
            
            private function practitionerClear():void
            {
            	namePractitioner.text	=	"";
            	emailPractitioner.text	=	"";
            	passwordPractitioner.text	=	"";
            	retryPasswordPractitioner.text	=	"";
            	
            	resultNamePractitioner.text	=	"";
				resultEmailPractitioner.text		=	"";
				resultPasswordPractitioner.text	=	"";
				resultRetryPasswordPractitioner.text	=	"";
            	
            	model.result	=	"";
            }